home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Super Collection / Windows 95 Super Collection.iso / win95 / bench / thread / fpcalc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-13  |  4.4 KB  |  192 lines

  1. //==========================================================================
  2. // FPCALC.C - C routines that run a floating-point intensive operation.
  3. //             (Change this code as desired to implement your own floating-
  4. //                 point benchmark.)
  5. //
  6. //                    Note: This floating-point calculation (a Fast Fourier 
  7. //                            transformation is borrowed from the 
  8. //                          public domain Stanford UNIX benchmarks.)
  9. //                            
  10. //                            DoFloatingPointCalc() is the entry point here,
  11. //                   which is called by each thread procedure. 
  12. //
  13. //==========================================================================
  14.  
  15. #include <stdlib.h>
  16. #include "fpcalc.h"
  17.  
  18. UINT DoFloatingPointCalc(UINT uReps, UINT uRepsThisPass, UINT uCurrentReps)
  19. {
  20.     // Here, FP calculation is a fast Fourier transformation
  21.     //       using matrices of complex numbers.
  22.     UINT i = 0;
  23.  
  24.     while(1)
  25.     {
  26.         Oscar();
  27.  
  28.          i++;
  29.         if(i >= uRepsThisPass || i + uCurrentReps >= uReps)
  30.             break;
  31.     }
  32.  
  33.     return i + uCurrentReps;
  34. }
  35.  
  36. // Fast Fourier transform define's
  37. #define fftsize           256    
  38. #define fftsize2          129    
  39.  
  40. //----------------------------------------------------------
  41. //  Fast Fourier Transformation (Oscar)             
  42. //----------------------------------------------------------
  43. //----------------------------------------------------------
  44. // Cos() computes cos of x (x in radians) by an expansion. 
  45. //----------------------------------------------------------
  46. float Cos (float x)
  47. {
  48.     int i, factor;
  49.     float    result,power;
  50.  
  51.    result = (float)1.0; factor = 1;  power = x;
  52.    for ( i = 2; i <= 10; i++ ) 
  53.    {
  54.       factor = factor * i;  power = power*x;
  55.       if ( (i & 1) == 0 )  
  56.       {
  57.              if ( (i & 3) == 0 ) result = result + power/factor;
  58.              else result = result - power/factor;
  59.          }
  60.    }
  61.    return (result);
  62. }
  63.  
  64. //----------------------------------------------------------
  65. // Uniform11()
  66. //----------------------------------------------------------
  67. void Uniform11(long iy, float yfl) 
  68. {
  69.     iy = (4855*iy + 1731) & 8191;
  70.     yfl = (float)iy/(float)8192.0;
  71. }
  72.  
  73. //----------------------------------------------------------
  74. // Exptab()
  75. //----------------------------------------------------------
  76. void Exptab(int n, struct complex e[]) 
  77.     float theta, divisor, h[26];
  78.    int i, j, k, l, m;
  79.  
  80.    theta = (float)3.1415926536;
  81.    divisor = (float)4.0;
  82.    for ( i=1; i <= 25; i++ )
  83.    {
  84.        h[i] = 1/(2*Cos( theta/divisor ));
  85.       divisor = divisor + divisor;
  86.    }
  87.  
  88.    m = n / 2 ;
  89.    l = m / 2 ;
  90.    j = 1 ;
  91.    e[1].rp = (float)1.0 ;
  92.    e[1].ip = (float)0.0;
  93.    e[l+1].rp = (float)0.0;
  94.    e[l+1].ip = (float)1.0 ;
  95.    e[m+1].rp = (float)-1.0 ;
  96.    e[m+1].ip = (float)0.0 ;
  97.  
  98.    do 
  99.    {
  100.        i = l / 2 ;
  101.       k = i ;
  102.          do 
  103.          {
  104.              e[k+1].rp = h[j]*(e[k+i+1].rp+e[k-i+1].rp) ;
  105.              e[k+1].ip = h[j]*(e[k+i+1].ip+e[k-i+1].ip) ;
  106.              k = k+l ;
  107.          } 
  108.          while ( k <= m );
  109.  
  110.          j = min( j+1, 25);
  111.          l = i ;
  112.    } 
  113.    while ( l > 1 );
  114.  
  115.   
  116. //----------------------------------------------------------
  117. // Fft()
  118. //----------------------------------------------------------
  119. void Fft( int n, struct complex z[], struct complex w[], struct complex e[], float sqrinv)
  120. {
  121.     int i, j, k, l, m, index;
  122.    m = n / 2 ;
  123.    l = 1 ;
  124.  
  125.    do 
  126.    {
  127.          k = 0 ;
  128.          j = l ;
  129.          i = 1 ;
  130.  
  131.         do 
  132.         {
  133.             do 
  134.             {
  135.              w[i+k].rp = z[i].rp+z[m+i].rp ;
  136.              w[i+k].ip = z[i].ip+z[m+i].ip ;
  137.              w[i+j].rp = e[k+1].rp*(z[i].rp-z[i+m].rp)
  138.                  -e[k+1].ip*(z[i].ip-z[i+m].ip) ;
  139.              w[i+j].ip = e[k+1].rp*(z[i].ip-z[i+m].ip)
  140.                  +e[k+1].ip*(z[i].rp-z[i+m].rp) ;
  141.                  i = i+1 ;
  142.              } 
  143.              while ( i <= j );
  144.          
  145.            k = j ;
  146.              j = k+l ;
  147.          } 
  148.          while ( j <= m );
  149.  
  150.          index = 1;
  151.          do 
  152.          {
  153.              z[index] = w[index];
  154.              index = index+1;
  155.          } 
  156.          while ( index <= n );
  157.          l = l+l ;
  158.    } 
  159.    while ( l <= m );
  160.  
  161.      for ( i = 1; i <= n; i++ )
  162.      {
  163.          z[i].rp = sqrinv*z[i].rp ;
  164.          z[i].ip = -sqrinv*z[i].ip;
  165.      } 
  166.  
  167. void Oscar(void)
  168.     struct complex z[fftsize+1], w[fftsize+1], e[fftsize2+1];
  169.     float    zr, zi;
  170.     
  171.     int i;
  172.     long seed = 5767 ;
  173.  
  174.     Exptab(fftsize,e) ;
  175.     for ( i = 1; i <= fftsize; i++ )
  176.     {
  177.        Uniform11( seed, zr );
  178.         Uniform11( seed, zi );
  179.         z[i].rp = (float)20.0*zr - (float)10.0;
  180.         z[i].ip = (float)20.0*zi - (float)10.0;
  181.    }
  182.  
  183.     for ( i = 1; i <= 20; i++ )
  184.     {
  185.       Fft(fftsize,z,w,e,(float)0.0625) ;
  186.       }
  187.